package com.uzhie.web.controller.http;


import com.alibaba.fastjson.JSONObject;
import com.uzhie.exception.UserException;
import com.uzhie.web.bean.PageBean;
import com.uzhie.web.bean.PayloadBean;
import com.uzhie.web.controller.http.comm.CommController;
import com.uzhie.web.module.ReportLastPosition;
import com.uzhie.web.module.ReportOffLine;
import com.uzhie.web.service.PassengerService;
import com.uzhie.web.service.report.LastPositionReportService;
import com.uzhie.web.service.report.OffLineReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "report")
public class ReportController extends CommController {

    @Autowired
    LastPositionReportService lastPositionReportService;
    @Autowired(required = false)
    OffLineReportService offLineReportService;
    @Autowired
    PassengerService passengerService;

    /**
     * 查询告警处理统计报表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryWarnsReport")
    public PayloadBean queryWarnsReport(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean pageBean) {
        return null;
    }

    /**
     * 查询车辆在线率统计报表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryOnlineReport")
    public PayloadBean queryOnlineReport(@RequestBody(required = false) JSONObject body) {
        return null;
    }

    /**
     * 车辆最后离线时间统计报表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryOfflineReport")
    public PayloadBean queryOfflineReport(@RequestBody(required = false) JSONObject body) {
//        List<String> departIds = JSONArray.parseArray(body.getJSONArray("departIds").toJSONString(), String.class);
        String userId = body.getString("userId");
        ReportOffLine reportOffLine = offLineReportService.queryReportByVehicleNumber(body.getString("vehicleNumber"), body.getString("departmentName"), userId);
        List<ReportOffLine> result = new ArrayList<>();
        if (reportOffLine != null) result.add(reportOffLine);
        return buildResponse(result);
    }

    /**
     * 查询流量统计报表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryFlowReport")
    public PayloadBean queryFlowReport(@RequestBody(required = false) JSONObject body) {
        return null;
    }

    /**
     * 最后定位位置统计报表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryLastPosition")
    public PayloadBean queryLastPosition(@RequestBody(required = false) JSONObject body) {
//        List<String> departId = JSONArray.parseArray(body.getJSONArray("departIds").toJSONString(), String.class);
        String userId = body.getString("userId");
        List<ReportLastPosition> result = lastPositionReportService.queryLastPositionReport(body.getString("vehicleNumber"), userId);
        return buildResponse(result);
    }

    /**
     * 存储空间报表
     *
     * @return
     */
    public PayloadBean queryLocation() {
        return null;
    }

    /**
     * 下发指令统计报表
     *
     * @return
     */
    public PayloadBean queryOrderReport() {
        return null;
    }

    /**
     * 查询客户端使用统计报表
     *
     * @return
     */
    public PayloadBean queryClientReport() {
        return null;
    }


    /**
     * 查询客流量报表
     *
     * @param body
     * @param pageBean
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryPassengerReport")
    public PayloadBean queryPassengerReport(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean pageBean) {
        try {
            return buildResponseWithPage(passengerService.selectListByConidition(body.getString("userId"), body.getString("vehicleNumber"),
                    body.getString("startTime"), body.getString("endTime"), pageBean.getRowBounds()),pageBean);
        } catch (UserException e) {
            return buildResponseWithPage(null,pageBean, e.getMessage());
        }
    }
}
